package cn.wolfcode.service.impl;

import cn.wolfcode.common.constants.CommonConstants;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OperateIntegralVo;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayLog;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.mapper.OrderInfoMapper;
import cn.wolfcode.mapper.PayLogMapper;
import cn.wolfcode.mapper.RefundLogMapper;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.DateUtil;
import cn.wolfcode.util.IdGenerateUtil;
import cn.wolfcode.web.config.UserPhone;
import cn.wolfcode.web.feign.IntegralFeignApi;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * Created by wolfcode-hxj
 */
@Service
public class OrderInfoSeviceImpl implements IOrderInfoService {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private PayLogMapper payLogMapper;
    @Autowired
    private RefundLogMapper refundLogMapper;

    @Override
    @Transactional
    public String doSeckill(Long seckillId, String phone) {

        //扣减库存
        seckillProductService.decrStock(seckillId);
        //创建秒杀订单
        String orderNo=createOrder(seckillId,phone);
        return orderNo;
    }

    @Override
    public OrderInfo find(String orderNo) {

        return orderInfoMapper.find(orderNo);
    }

    private String createOrder(Long seckillId, String phone) {
        SeckillProduct seckillProductVo = seckillProductService.find(seckillId);
        OrderInfo orderInfo=new OrderInfo();
        BeanUtils.copyProperties(seckillProductVo,orderInfo);
        orderInfo.setOrderNo(IdGenerateUtil.get().nextId()+"");
//        orderInfo.setPhone(phone);
        orderInfo.setCreateDate(new Date());
        orderInfo.setSeckillTime(seckillProductVo.getTime());
        orderInfo.setSeckillId(seckillProductVo.getId());
        orderInfoMapper.insert(orderInfo);
        return orderInfo.getOrderNo();
    }
}
